package orders

import (
	"context"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"github.com/spf13/cast"
	"insighttracking/apps/admin/auth"
	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"
	"insighttracking/common/enum"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/service"
	"net/http"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetOrdersPageApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetOrdersPageApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetOrdersPageApiLogic {
	hostname, _ := os.Hostname()
	return &GetOrdersPageApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetOrdersPageApiLogic) GetOrdersPageApi(req *types.GetOrdersPageReq) (resp *types.GetOrdersPageResp, err error) {
	// todo: add your logic here and delete this line
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return nil, err
	}
	var customerId = req.CustomerId
	if accountInfo.AccountType == enum.EnterpriseCustomerEnum {
		customerId = cast.ToString(accountInfo.CustomerId)
	}
	ordersBo, count, err := service.OrdersService.GetPageApi(l.ctx, bo.OrdersPageBoReq{
		EnterpriseID:     accountInfo.EnterpriseId,
		OrderNo:          req.OrderNo,          // 订单流水号
		Status:           req.Status,           // 订单状态,0表示已下单(待采购),10分拣中，20已发货,30已完成,40取消订单
		StartDate:        req.StartDate,        // 开始时间，时间格式为:2024-12-12
		EndDate:          req.EndDate,          // 结束时间, 时间格式为:2024-12-12
		Name:             req.Name,             // 商品名称
		CustomerName:     req.CustomerName,     // 门店名称
		SettlementStatus: req.SettlementStatus, // 结算状态，0表示未结算,1表示已结算
		CustomerId:       customerId,           // 客户id
		PageSize:         req.PageSize,         // 当前条数
		PageNumber:       req.PageNumber,       // 当前页数
	})
	if err != nil || len(ordersBo) == 0 {
		return &types.GetOrdersPageResp{
			Data: make([]types.GetOrdersResp, 0),
			PageBaseResp: types.PageBaseResp{
				Total:      0,
				PageSize:   req.PageSize,
				PageNumber: req.PageNumber,
			},
		}, nil
	}
	var result = make([]types.GetOrdersResp, 0)
	for _, item := range ordersBo {
		result = append(result, types.GetOrdersResp{
			ID:                 item.ID,                                                                           // 主键id
			OrderNo:            item.OrderNo,                                                                      // 订单流水号
			EnterpriseID:       item.EnterpriseID,                                                                 // 关联到enterprise表主键id
			CustomerID:         item.CustomerID,                                                                   // 客户id,关联到enterprise_customer表主键id
			CustomerName:       item.CustomerName,                                                                 // 门店名称
			SendGoodsDate:      k.If(item.SendGoodsDate.IsZero(), 0, utils.TimestampToString(item.SendGoodsDate)), // 发货日期
			SendGoodsMethod:    item.SendGoodsMethod,                                                              // 发货方式,0表示司机配送
			SettlementMethod:   item.SettlementMethod,                                                             // 结算方式，0表示账期
			PurchaserID:        item.PurchaserID,                                                                  // 采购员,关联到
			PurchaserName:      item.PurchaserName,                                                                // 采购员名称
			Remark:             item.Remark,                                                                       // 订单备注
			TotalAmount:        item.TotalAmount.StringFixed(2),                                                   // 订单金额
			TotalCount:         item.TotalCount.StringFixed(2),                                                    // 订单商品数量
			Status:             item.Status,                                                                       // 订单状态,0表示已下单(待采购),1表示采购中,2表示已采购,3待入库，4已入库，5已发货,6已收货,7表示退货
			ReceiveGoodsName:   item.ReceiveGoodsName,                                                             // 收货员姓名
			ReceiveGoodsMobile: item.ReceiveGoodsMobile,                                                           // 收货人手机号码
			ReceiveAddress:     item.ReceiveAddress,                                                               // 收货人地址
			IsPurchasingPlan:   item.IsPurchasingPlan,                                                             // 是否发布采购计划,0表示不发布,1表示发布
			Source:             item.Source,                                                                       // 订单来源,0表示app端,1表示pc下单
			SettlementStatus:   item.SettlementStatus,                                                             // 结算状态，0表示未结算,1表示已结算
			SendGoodsID:        item.SendGoodsID,                                                                  // 配置司机,关联到account表主键id
			SendGoodsName:      item.SendGoodsName,                                                                // 配送司机名称
			WarehouseID:        item.WarehouseID,                                                                  // 发货仓库,关联到warehouse表主键id
			WarehouseName:      item.WarehouseName,                                                                // 仓库名称
			CreatedAt:          utils.TimestampToString(item.CreatedAt),                                           // 创建时间
			UpdatedAt:          utils.TimestampToString(item.UpdatedAt),                                           // 更新时间
		})
	}
	return &types.GetOrdersPageResp{
		Data: result,
		PageBaseResp: types.PageBaseResp{
			Total:      count,
			PageSize:   req.PageSize,
			PageNumber: req.PageNumber,
		},
	}, nil
}
